package cn.tedu.baidashi.service.Impl;

import cn.tedu.baidashi.exception.ServiceException;
import cn.tedu.baidashi.mapper.CategoryMapper;
import cn.tedu.baidashi.mapper.ItemsMapper;
import cn.tedu.baidashi.model.Category;
import cn.tedu.baidashi.model.Items;
import cn.tedu.baidashi.service.ICategoryService;
import cn.tedu.baidashi.vo.ItemsInfoVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 查询分类名称
     * @return
     */
    @Override
    public List<Category> getCategory() {
        System.out.println("执行查询");
        List<Category> getCategory = categoryMapper.getCategory();
        System.out.println("查询结果"+getCategory);
        return getCategory;
    }

    /**
     * 根据分类id查询商品
     * @param categoryId
     * @return
     */
    @Override
    public List<ItemsInfoVO> getItemsById(Integer categoryId) {
        List<ItemsInfoVO> itemsInfoVOS = categoryMapper.findItemsById(categoryId);
        String category = categoryMapper.findCategoryNameById(categoryId);
        for (ItemsInfoVO items : itemsInfoVOS){
            items.setCatName(category);
        }
        return itemsInfoVOS;
    }

    @Override
    public String category(Integer categoryId) {
        String category = categoryMapper.findCategoryNameById(categoryId);
        return category;
    }

    /**
     *
     * @return
     */
    @Override
    public List<ItemsInfoVO> findItems() {
        List<ItemsInfoVO> itemsInfoVOS = categoryMapper.findItems();
        return itemsInfoVOS;
    }


    @Resource
    private ItemsMapper itemsMapper;

    /**
     * 获得分类集合
     * @return
     */
    @Override
    public List<Category> findCategories() {
        return categoryMapper.findCategories();
    }

    /**
     * 根据分类id修改分类名称
     * @param catId
     * @param catName
     * @return
     */
    @Override
    public Category updateCategoryById(Integer catId, String catName, LocalDateTime createdTime) {
        Category category=categoryMapper.findCategoryByName(catName);
        if (category == null){
            int num=categoryMapper.updateCategoryById(catId,catName,createdTime);
            if (num != 1){
                throw new ServiceException("数据库错误，修改失败");
            }
            return categoryMapper.findCategoryById(catId);
        }else {
            throw new ServiceException("已存在相同名称的分类");
        }

    }

    /**
     * 根据分类id删除分类
     * @param catId
     */
    @Override
    public void deleteCategoryById(Integer catId) {
        List<Items> items=itemsMapper.findItemsByCatId(catId);
        if (items.isEmpty()){
            int num=categoryMapper.deleteCategoryById(catId);
            if (num != 1){
                throw  new ServiceException("数据库错误，删除失败");
            }
        }else {
            throw new ServiceException("该商品分类还有关联的商品信息，无法删除");
        }

    }

    /**
     * 新增类别
     * @param category
     * @return 新增的category
     */
    @Override
    public Category addCategory(Category category) {
        Category cate=categoryMapper.findCategoryByName(category.getCatName());
        if (cate == null){
            int num=categoryMapper.addCategory(category);
            if (num!=1){
                throw  new ServiceException("数据库错误，新增失败");
            }
            return categoryMapper.findCategoryById(category.getId());
        }else {
            throw new ServiceException("已存在相同名称的分类");
        }

    }


}
